home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n08.arc / CHANGE.BAT < prev    next >
DOS Batch File  |  1991-04-05  |  10KB  |  246 lines

  1. @ECHO OFF
  2. IF '%3'=='' GOTO usage
  3. CLS
  4. ECHO CHANGE.BAT
  5. ECHO.
  6. ECHO File to change: "%1"
  7. ECHO Search for    : "%2"
  8. ECHO Replace with  : "%3"
  9. ECHO.
  10. IF NOT EXIST %1 GOTO filenotfound
  11. ECHO The two strings should be the same length.  Are they?
  12. ECHO.
  13. ECHO To abort the process, press Ctrl-C.  Otherwise...
  14. PAUSE
  15. ECHO Let me make sure everything's correct and ready...
  16.  
  17.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  18.      :@  Set up the necessary environment vars.     @:
  19.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  20.  
  21. SET length=XXXX
  22. SET address=XXXX:XXXX
  23. SET oversize=XXXX
  24. SET times=XX
  25.   REM -------------------------------------------
  26.   REM   Check that there was enough environment space
  27.   REM -------------------------------------------
  28. IF NOT '%times%'=='XX' GOTO environment
  29.  
  30.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  31.      :@  Make sure the file isn't EXE or HEX        @:
  32.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  33.  
  34. ATTRIB %1 | FIND ".EXE" > TEMP01.CHG
  35. ATTRIB %1 | FIND ".HEX" >> TEMP01.CHG
  36. COPY TEMP01.CHG TEMP02.CHG > NUL
  37. DEL TEMP01.CHG
  38.   REM -------------------------------------------
  39.   REM   If the file is NOT an EXE or HEX file, TEMP01.CHG will be a
  40.   REM   zero-length file.  COPY will not copy a zero-length file.
  41.   REM   So if TEMP02.CHG does NOT exist, all is well.
  42.   REM -------------------------------------------
  43. IF EXIST TEMP02.CHG GOTO EXEorHEX
  44.  
  45.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  46.      :@  Check that the file is 64K or smaller      @:
  47.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  48.  
  49. ECHO RBX>SCRIPT00.CHG
  50. ECHO. >> SCRIPT00.CHG
  51. ECHO Q >> SCRIPT00.CHG
  52.   REM -------------------------------------------
  53.   REM  Running SCRIPT00.CHG creates batch file CHANGE00.BAT.  It will
  54.   REM  contains a single line of the form "BX ####", where #### is
  55.   REM  the number of full 64K segments in the file.
  56.   REM -------------------------------------------
  57. DEBUG %1 < SCRIPT00.CHG | FIND "BX " | FIND /V "RBX" > CHANGE00.BAT
  58.   REM -------------------------------------------
  59.   REM   Create BX.BAT -- CHANGE00.BAT will run BX.BAT
  60.   REM -------------------------------------------
  61. ECHO @SET oversize=%%1>BX.BAT
  62.   REM -------------------------------------------
  63.   REM   Call CHANGE00.BAT, which runs BX.BAT, which puts the
  64.   REM   number of full 64K segments into the e-var "oversize"
  65.   REM -------------------------------------------
  66. CALL CHANGE00
  67. IF NOT '%oversize%'=='0000' GOTO toobig
  68. ECHO OK, looks good, now to patch the file...
  69.  
  70.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  71.      :@  Get the file's size into an e-var          @:
  72.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  73.  
  74. ECHO RCX> SCRIPT01.CHG
  75. ECHO. >> SCRIPT01.CHG
  76. ECHO Q >> SCRIPT01.CHG
  77.   REM -------------------------------------------
  78.   REM  Running SCRIPT01.CHG creates batch file CHANGE01.BAT.  It will
  79.   REM  contains a single line of the form "CX ####", where #### is
  80.   REM  the size of the file.
  81.   REM -------------------------------------------
  82. DEBUG %1 < SCRIPT01.CHG | FIND "CX " | FIND /V "RCX" > CHANGE01.BAT
  83.   REM -------------------------------------------
  84.   REM   Create CX.BAT -- CHANGE01.BAT executes CX.BAT
  85.   REM -------------------------------------------
  86. ECHO @SET length=%%1>CX.BAT
  87.   REM -------------------------------------------
  88.   REM   Call CHANGE01, which calls CX.BAT, which puts the file length
  89.   REM   into the environment variable "length
  90.   REM -------------------------------------------
  91. CALL CHANGE01
  92.  
  93.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  94.      :@  Create several useful temporary files      @:
  95.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  96.  
  97.   REM -------------------------------------------
  98.   REM   Create DEBUG script that gets the address of the search
  99.   REM   string.  It _may_ get multiple addresses -- that's OK.
  100.   REM -------------------------------------------
  101. ECHO S CS:100 L %length% '%2' > SCRIPT02.CHG
  102. ECHO Q >> SCRIPT02.CHG
  103.   REM -------------------------------------------
  104.   REM   Create --------.BAT, which is called by CHANGE04.BAT to get
  105.   REM   the number of times the file appears into e-var "times"
  106.   REM -------------------------------------------
  107. ECHO @SET times=%%2>--------.BAT
  108.   REM -------------------------------------------
  109.   REM   Create and run a DEBUG script.  When run, it produces the file
  110.   REM   DATA01.CHG, containing the string "CHANGE07 " with no carriage
  111.   REM   return or line feed.
  112.   REM -------------------------------------------
  113. ECHO NDATA01.CHG>CHANGE04.SCR
  114. ECHO E 100 'CHANGE07 '>>CHANGE04.SCR
  115. ECHO RCX>>CHANGE04.SCR
  116. ECHO 9>>CHANGE04.SCR
  117. ECHO W>>CHANGE04.SCR
  118. ECHO Q>>CHANGE04.SCR
  119. DEBUG < CHANGE04.SCR > NUL
  120.   REM -------------------------------------------
  121.   REM   Create CHANGE07, which is called by CHANGE02 to get the address
  122.   REM   of first occurrence of search string into e-var "address".
  123.   REM -------------------------------------------
  124. ECHO @SET address=%%1 > CHANGE07.BAT
  125.  
  126.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  127.      :@  Finally, create and run a script that      @:
  128.      :@  performs the replacement.                  @:
  129.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  130.  
  131. :notfirst
  132.  
  133.   REM -------------------------------------------
  134.   REM   Running SCRIPT02.CHG finds address(es) of search string
  135.   REM -------------------------------------------
  136. DEBUG %1 < SCRIPT02.CHG | FIND /V "%2" | FIND ":" > TEMP03.CHG
  137.   REM -------------------------------------------
  138.   REM   FIND with the /C (for Count) switch produces output of
  139.   REM   the form "---------- TEMPO3.CHG: #", where # is the 
  140.   REM   number of times the search string occurs in the file.
  141.   REM -------------------------------------------
  142. FIND /C ":" TEMP03.CHG > CHANGE04.BAT
  143.   REM -------------------------------------------
  144.   REM   Call CHANGE04, which calls --------.BAT
  145.   REM -------------------------------------------
  146. CALL CHANGE04
  147.   REM -------------------------------------------
  148.   REM   If the search string is not present, get out!
  149.   REM -------------------------------------------
  150. IF '%times%'=='0' GOTO notfound
  151.   REM -------------------------------------------
  152.   REM   Pre-pend DATA01.CHG to the first line of the file of addresses
  153.   REM   to create CHANGE02.BAT.  Running CHANGE02.BAT puts the address
  154.   REM   of the first occurrence of the search string into an e-var.
  155.   REM -------------------------------------------
  156. COPY DATA01.CHG + TEMP03.CHG CHANGE02.BAT > NUL
  157. CALL CHANGE02
  158.   REM -------------------------------------------
  159.   REM   Create DEBUG script that overwrites the data at the address
  160.   REM   of the search string with the replace string.
  161.   REM -------------------------------------------
  162. ECHO E %address% '%3' > SCRIPT03.CHG
  163. ECHO W >> SCRIPT03.CHG
  164. ECHO Q >> SCRIPT03.CHG
  165.   REM -------------------------------------------
  166.   REM   Perform the replacement and look for errors.
  167.   REM -------------------------------------------
  168. DEBUG %1 < SCRIPT03.CHG | FIND "Error" > TEMP04.CHG
  169. COPY TEMP04.CHG TEMP05.CHG > NUL
  170. DEL TEMP04.CHG
  171.   REM -------------------------------------------
  172.   REM   If there were no errors, TEMP04.CHG is a zero-length file.
  173.   REM   DOS will not copy a zero-length file.  So if the copy does
  174.   REM   NOT create TEMP05.CHG, all is well.
  175.   REM -------------------------------------------
  176. IF EXIST TEMP05.CHG GOTO trouble
  177.  
  178.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  179.      :@  If the search string occurred more than    :
  180.      :@  once, repeat the process.                  @:
  181.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  182.  
  183. IF '%times%'=='1' GOTO success
  184. ECHO The string "%2" appears in the file %1 %times% times.
  185. ECHO Replacing next occurrence...
  186. GOTO notfirst
  187.  
  188.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  189.      :@  Here is the code for ending the batch      @:
  190.      :@  file, successfully or otherwise.           @:
  191.      :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
  192.  
  193. :success
  194. ECHO.
  195. ECHO I have successfully patched the file %1 to replace all
  196. ECHO occurrences of "%2" with "%3".  I will now delete all
  197. ECHO of the temporary files created in the process.
  198. :cleanup
  199. IF EXIST SCRIPT0?.CHG DEL SCRIPT0?.CHG > NUL
  200. IF EXIST TEMP0?.CHG   DEL TEMP0?.CHG > NUL
  201. IF EXIST CHANGE0?.*   DEL CHANGE0?.* > NUL
  202. IF EXIST CX.BAT       DEL CX.BAT > NUL
  203. IF EXIST BX.BAT       DEL BX.BAT > NUL
  204. IF EXIST --------.BAT DEL --------.BAT > NUL
  205. IF EXIST DATA01.CHG   DEL DATA01.CHG > NUL
  206. FOR %%v IN (length address times oversize) DO SET %%v=
  207. GOTO end
  208.  
  209. :filenotfound
  210. ECHO You're asking me to change the file "%1", but I don't
  211. ECHO SEE any file with that name!
  212. GOTO end
  213.  
  214. :toobig
  215. ECHO Sorry, CHANGE.BAT only works on files 64K or smaller
  216. :GOTO cleanup
  217. :notfound
  218. ECHO Sorry, the file %1 does not contain the string "%2".
  219. GOTO cleanup
  220. :usage
  221. ECHO USAGE: CHANGE filename searchstring replacestring
  222. ECHO.
  223. ECHO The file to be changed must be 64K or less in size, and it
  224. ECHO must not have an EXE or HEX extension.  Also, the search string
  225. ECHO and the replace string should be the same size.
  226. GOTO end
  227.  
  228. :EXEorHEX
  229. ECHO Sorry, you cannot use CHANGE on a .EXE or .HEX file.
  230. ECHO to have another extension before running CHANGE.
  231. GOTO cleanup
  232.  
  233. :environment
  234. ECHO Sorry, you do not have enough available environment space
  235. ECHO to run CHANGE.BAT.
  236. GOTO cleanup
  237.  
  238. :trouble
  239. DEL TEMP05.CHG
  240. ECHO There was trouble in the final DEBUG process.  Examine the
  241. ECHO files created by CHANGE.BAT to see where the problem started.
  242. ECHO Also, type SET and look at environment variables "oversize", 
  243. ECHO "address", "length", and "times".
  244.  
  245. :end
  246.